<style>
#ParamQueryTable{
	margin: 4px 0px;
}
#ParamIdText{
	margin-right: 8px;
}
#ParamNameText{
	margin-right: 8px;
}
#QueryParamRecordButton{
	margin-right: 8px;
}
#AddParamRecordButton{
	color: #090;
	margin-left: 16px;
}
#ReloadConfigButton{
	color: #D00;
	margin-left: 8px;
}
</style>
<div id='ParamRecordDiv'>
	<table id='ParamQueryTable'>
		<tr>
			<td>
				<v-text id='ParamIdText' title='参数编号' maxlength='24'></v-text>
				<v-text id='ParamNameText' title='参数名称' maxlength='24'></v-text>
				<v-button id='QueryParamRecordButton' title='查询'></v-button>
				<v-button id='AddParamRecordButton' title='添加参数'></v-button>
				<v-button id='ReloadConfigButton' title='更新配置'></v-button>
			</td>
		</tr>
	</table>
	<div id='ParamListDiv'></div>
</div>

<script>
getVue('ParamQueryTable');

var vmdata = {
	title: {
		id: centerText('参数编号', 'bold'),
		name: '参数名称',
		param: '参数内容',
		remark: '参数说明'
	},
	filter: {
		id: centerText
	},
	button: [{
			title: '编辑',
			click: editParamRecord
		},{
			title: '删除',
			click: removeParamRecord
		}
	]
};

var listview = new RecordListView('ParamListDiv', '/GetParamList', vmdata, function(){
	return {id: $('#ParamIdText').val(), name: $('#ParamNameText').val()};
});

$('#ParamIdText').keyup(function(){
	listview.reload(false);
});

$('#ParamNameText').keyup(function(){
	listview.reload(false);
});

$('#QueryParamRecordButton').click(function(){
	listview.reload(true);
});

$('#ReloadConfigButton').click(function(){
	showConfirmMessage('是否要重新加载配置文件？', '更新配置', function(flag){
		if (flag){
			showToastMessage('正在重新加载配置文件...', true);

			getHttpResult('/ExecModule', {cmd: 'reload'}, function(data){
				showToast('加载配置文件成功');
			});
		}
	});
});

$('#AddParamRecordButton').click(function(){
	var data = {
		title: ['参数编号', '参数名称', '参数过滤', '参数备注'],
		model: {id: '', name: '', filter: '', remark: ''},
		style: [
			{size: 24, minlength: 4, maxlength: 32},
			{size: 24, minlength: 4, maxlength: 32},
			{size: 24, minlength: 0, maxlength: 128, type: 'textarea'},
			{size: 24, minlength: 0, maxlength: 128, type: 'textarea'}
		]
	};

	var elem = showConfirmDialog(data, '添加参数', function(flag){
		if (flag){
			data.model['flag'] = 'A';
			
			getHttpResult('/EditParam', data.model, function(data){
				if (data.code == XG_TIMEOUT){
					sessionTimeout();
				}
				else if (data.code == XG_DUPLICATE){
					showToast('参数已存在');
				}
				else if (data.code < 0){
					showToast('添加参数失败');
				}
				else{
					$('#ParamIdText').val('');
					$('#ParamNameText').val('');
					$('#QueryParamRecordButton').click();
				}
			});
		}
	});

	$(elem.remark).width($(elem.id).width());
	$(elem.filter).width($(elem.id).width()).attr('placeholder', "输入以'/'开头正则表达式或取值范围如(0,1,2,3,4...)");
});

function removeParamRecord(item){
	showConfirmMessage('是否要删除[' + item.name + ']参数？', '删除参数', function(flag){
		if (flag){
			getHttpResult('/EditParam', {flag: 'D', id: item.id}, function(data){
				if (data.code ==  XG_TIMEOUT){
					sessionTimeout();
				}
				else if (data.code < 0){
					showToast('删除参数失败');
				}
				else{
					showToast('删除参数成功');
					listview.reload(false);
				}
			});
		}
	});
}

function editParamRecord(item){
	var vmdata = {
		title: ['参数内容', '参数备注'],
		model: {param: item.param, remark: item.remark},
		style: [
			{minlength: 1, maxlength: 128, type: 'textarea'},
			{minlength: 1, maxlength: 128, type: 'textarea'}
		]
	};

	var filter = item.filter;

	if (filter.length > 3 && filter.substr(0, 1) == '(' && filter.substr(filter.length - 1) == ')'){
		var valist = filter.substr(1, filter.length - 2).split(',');
		if (valist.length > 0) vmdata.style[0] = {select: valist.join('|')};
	}

	showConfirmDialog(vmdata, '修改参数', function(flag){
		if (flag){
			vmdata.model['flag'] = 'U';
			vmdata.model['id'] = item.id;

			getHttpResult('/EditParam', vmdata.model, function(data){
				if (data.code == XG_TIMEOUT){
					sessionTimeout();
				}
				else if (data.code < 0){
					showToast('修改参数失败');
				}
				else{
					item.remark = vmdata.model.remark;
					item.param = vmdata.model.param;
					listview.update();

					showToast('修改参数成功');
				}
			});
		}
	});
}
</script>